package ch.hslu.weather;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class HttpGET {

	public HttpGET() {
	}

	public static InputStream send(String url) {
		HttpClient httpClient = new DefaultHttpClient();

		StringBuilder uriBuilder = new StringBuilder(url);

		HttpGet request = new HttpGet(uriBuilder.toString());

		try {
			HttpResponse response = httpClient.execute(request);

			int status = response.getStatusLine().getStatusCode();

			Log.i("HTTP CLIENT", "Status: " + status);
			if (status != HttpStatus.SC_OK) {
				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
				response.getEntity().writeTo(ostream);
				Log.e("HTTP CLIENT", ostream.toString());
			} else {
				return response.getEntity().getContent();
			}
		} catch (Exception e) {
			Log.e("HTTP CLIENT", e.getMessage());
		}

		return null;

	}
}
